<?php
declare (strict_types = 1);

namespace app\controller\backend\course;

use app\service\logic\common\course\ChapterService;
use app\validate\common\course\ChapterValidate;
use think\App;
use think\Request;
use think\Response;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class ChapterController extends BackendController
{
    protected $service;

    public function __construct(App $app, ChapterService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, ChapterValidate::class . '.index');

        $result = $this->service->getList($params, $search_fields, $search, ['byCreator']);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $params = $request->only($this->service->getSaveColumn());
        $params['admin_id'] = $request->middleware('admin')['id'];
        $this->validate($params, ChapterValidate::class . '.save');

        $result = $this->service->create($params);

        return amis($result);
    }

    /**
     * 显示资源
     *
     * @param $id
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read($id): Json
    {
        $result = $this->service->getSimpleDetail($id);
        return amis($result);
    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function update(Request $request): Json
    {
        $params = $request->only($this->service->getUpdateColumn());
        $this->validate($params, ChapterValidate::class . '.update');

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * 单项修改
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function item(Request $request): Json
    {
        $params = $request->only($this->service->getItemColumn());
        $this->validate($params, ChapterValidate::class . '.item');

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * 删除指定资源
     *
     * @param Request $request
     * @return Json
     */
    public function delete(Request $request): Json
    {
        $params = $request->only(['ids']);
        $result = $this->service->batchDelete($params);

        if ($result) {
            return amis();
        } else {
            return amis([], '操作失败', 417);
        }
    }
}
